package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.ApiResult;
import com.example.demo.common.MessageBody;
import com.example.demo.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName NoticeController
 * @Description
 * @date 2023/3/23 17:06
 * @Author yanceysong
 * @Version 1.0
 */

@Slf4j
@RestController
public class NoticeController {
    private static final SimpleDateFormat datetime_formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final ArrayList<String> list = new ArrayList<>();
    private final ArrayList<byte[]> byte_list = new ArrayList<byte[]>();


    @PostMapping("/message")
    public Object message(String operation, String param) {
        System.out.println("收到消息,operation:" + operation + "param:" + param);
        return new ApiResult(1, "hello");
    }

    @GetMapping("/testSendDelay")
    public String testSendDelay() {
        for (int i = 0; i < 50; i++) {
            HashMap<String, Object> param = new HashMap<>();
            param.put("operation", "123");
            param.put("param"
                    , JSONObject.toJSONString(new MessageBody(1
                            , "songyang"
                            , get_interval_date(i)
                            , "ACTION"
                            , "hello,world")));
            HttpUtil.post("http://localhost:5000/MsgSend", param);
        }
        return "ok";
    }

    @GetMapping("/testSend")
    public String testSend() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("operation", "123");
        param.put("param"
                , JSONObject.toJSONString(new MessageBody(1
                        , "songyang"
                        , ""
                        , "ACTION"
                        , "hello,world")));
        return HttpUtil.post("http://localhost:5000/MsgSend", param);
    }

    /**
     * 获取前几天的日期
     *
     * @param interval 前几天 -1是后一天 0 当天
     * @return 日期
     */
    private String get_interval_date(int interval) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, interval);
        date = calendar.getTime();
        System.out.println(datetime_formatter.format(date));
        return datetime_formatter.format(date);
    }

    @GetMapping("/add")
    public String add() {
        int length = 1 << 30;
        byte[] test = new byte[length];
        for (int i = 0; i < length; i++) {
            test[i] = 1;
        }
        byte_list.add(test);
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        return "ok" + String.format("%.2f", memoryMXBean.getHeapMemoryUsage().getUsed()
                / (1024 * 1024 * 1024d)) + "GB";
    }

    @GetMapping("/get")
    public String get() {
        return String.valueOf(list.size());
    }


//    private work_hour_and_operation check(work_hour_and_operation w){
//
//    }
}
